layui.use(['layer', 'element', 'laytpl', 'jquery', 'table', 'upload', 'form'], function() {
	//引入模块
	var layer = layui.layer,
		element = layui.element,
		laytpl = layui.laytpl,
		form = layui.form,
		upload = layui.upload,
		table = layui.table,
		$ = layui.jquery;

	if (localStorage.set != null) {
		//加载页面发生请求拿到数据
		$.ajax({
			url: "http://localhost:8080/sc/admin?type=home",
			async: true, //异步
			type: "POST",
			headers: {
				"token": localStorage.token
			},
			success: function(data) {
				//这里找id必须用js
				if (data.code == 'TokenFailed') {
					//删除token set
					delete localStorage.token;
					delete localStorage.set;
					layer.msg('登录失效，请重新登录！')
					setTimeout(function() {
						//调用父页面方法
						parent.tz();
					}, 1000);
				} else {
					var getTpl = document.getElementById('homemb').innerHTML,
						view = document.getElementById('homemoban');
					laytpl(getTpl).render(data, function(html) {
						view.innerHTML = html;
					});
				}
			}
		});

	} else {
		layer.msg('您还没有登录！')
		setTimeout(function() {
			parent.tz();
			//window.location.replace('admin_login.html');
		}, 1000);
	}
	//请求加载分析商品种类  和库存（饼图）
	$.ajax({
		url: "http://localhost:8080/sc/admin4?type=getSPZLkC",
		async: false, //异步
		type: "POST",
		headers: {
			"token": localStorage.token
		},
		success: function(data) {
			// 基于准备好的dom，初始化echarts实例
			var myChart = echarts.init(document.getElementById('kucun'));
			// 指定图表的配置项和数据
			var option = {
				tooltip: {
					trigger: 'item'
				},
				legend: {
					top: '0%',
					left: 'center'
				},
				series: [{
					name: '库存',
					type: 'pie',
					radius: ['30%', '70%'],
					avoidLabelOverlap: false,
					itemStyle: {
						borderRadius: 10,
						borderColor: '#fff',
						borderWidth: 2
					},
					label: {
						show: false,
						position: 'center'
					},
					emphasis: {
						label: {
							show: true,
							fontSize: '15',
							fontWeight: 'bold'
						}
					},
					labelLine: {
						show: false
					},
					data: data
				}]
			};
			// 使用刚指定的配置项和数据显示图表。
			myChart.setOption(option);
		}
	});

	//请求加载加入购物车的分类数量（饼图）
	$.ajax({
		url: "http://localhost:8080/sc/admin4?type=getSPZLC",
		async: false, //异步
		type: "POST",
		headers: {
			"token": localStorage.token
		},
		success: function(data) {
			// 基于准备好的dom，初始化echarts实例
			var myChart = echarts.init(document.getElementById('fenlei'));
			// 指定图表的配置项和数据
			var option = {
				tooltip: {
					trigger: 'item'
				},
				legend: {
					top: '0%',
					left: 'center'
				},
				series: [{
					name: '加入数量',
					type: 'pie',
					radius: ['30%', '70%'],
					avoidLabelOverlap: false,
					itemStyle: {
						borderRadius: 10,
						borderColor: '#fff',
						borderWidth: 2
					},
					label: {
						show: false,
						position: 'center'
					},
					emphasis: {
						label: {
							show: true,
							fontSize: '15',
							fontWeight: 'bold'
						}
					},
					labelLine: {
						show: false
					},
					data: data
				}]
			};
			// 使用刚指定的配置项和数据显示图表。
			myChart.setOption(option);
		}
	});

	//请求加载分析下单笔数（条形图）
	$.ajax({
		url: "http://localhost:8080/sc/admin4?type=getXOrderC",
		async: false, //异步
		type: "POST",
		headers: {
			"token": localStorage.token
		},
		success: function(data) {
			//转数组
			let dan = [];
			for (var i in data) {
				dan.push(data[i].value)
			}
			let month = [];
			for (var i in data) {
				month.push(data[i].name)
			}
			// 基于准备好的dom，初始化echarts实例
			var myChart = echarts.init(document.getElementById('XOrder'));
			// 指定图表的配置项和数据
			var option = {
				xAxis: {
					type: 'category',
					data: month
				},
				yAxis: {
					type: 'value'
				},
				series: [{
					data: dan,
					type: 'line',
					smooth: true
				}]
			};
			// 使用刚指定的配置项和数据显示图表。
			myChart.setOption(option);
		}
	});

	//请求加载统计下单次数最多的5个用户
	$.ajax({
		url: "http://localhost:8080/sc/admin4?type=getFiveUserXOrder",
		async: false, //异步
		type: "POST",
		headers: {
			"token": localStorage.token
		},
		success: function(data) {
			let user = [];
			for (var i in data) {
				user.push(data[i].name)
			}
			// 基于准备好的dom，初始化echarts实例
			var myChart = echarts.init(document.getElementById('FiveUser'));
			// 指定图表的配置项和数据
			var option = {
				tooltip: {
					trigger: 'item',
					formatter: "{a} <br/>{b} : {c}%"
				},
				legend: {
					data: user
				},

				series: [{
					name: '漏斗图',
					type: 'funnel',
					left: '10%',
					top: 60,
					//x2: 80,
					bottom: 60,
					width: '80%',
					// height: {totalHeight} - y - y2,
					min: 0,
					max: 100,
					minSize: '0%',
					maxSize: '100%',
					sort: 'descending',
					gap: 2,
					label: {
						show: true,
						position: 'inside'
					},
					labelLine: {
						length: 10,
						lineStyle: {
							width: 1,
							type: 'solid'
						}
					},
					itemStyle: {
						borderColor: '#fff',
						borderWidth: 1
					},
					emphasis: {
						label: {
							fontSize: 20
						}
					},
					data: data
				}]
			};
			// 使用刚指定的配置项和数据显示图表。
			myChart.setOption(option);
		}
	});
});
